package com.vp.org.web.servlet;

import com.vp.org.dao.IntegralExchangeDao;
import com.vp.org.dao.impl.IntegralExchangeDaoImpl;
import com.vp.org.pojo.IntegralExchange;
import com.vp.org.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@WebServlet("/exchange")
public class ExchangeServlet extends BaseServlet {

    private IntegralExchange integralExchange = null;
    private IntegralExchangeDao integralExchangeDao = null;

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        integralExchange = new IntegralExchange();
        integralExchangeDao = new IntegralExchangeDaoImpl();

        // 用BeanUtils封装
        try {
            BeanUtils.populate(integralExchange, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }

        if (integralExchangeDao.add(integralExchange) > 0) {
            // 添加成功
            request.setAttribute("addExchange",1);
            request.getRequestDispatcher("/admin/exchangeAdd.jsp").forward(request,response);

        } else {
            // 添加失败
            response.sendRedirect("/admin/error.jsp");
        }
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");

        integralExchange = new IntegralExchange();
        integralExchangeDao = new IntegralExchangeDaoImpl();

        integralExchange.setId(Integer.parseInt(id));
        if (integralExchangeDao.delete(integralExchange) > 0) {

            // 删除成功
            response.sendRedirect("http://localhost:8080/exchange?method=findByPage&pageIndex6=1");
        } else {
            // 删除失败
            response.sendRedirect("/admin/error.jsp");
        }
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        integralExchange = new IntegralExchange();
        integralExchangeDao = new IntegralExchangeDaoImpl();

        // 用BeanUtils封装
        try {
            BeanUtils.populate(integralExchange, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }

        if (integralExchangeDao.upadte(integralExchange) > 0) {
            // 修改成功

            // 地址重定向
            response.sendRedirect("/admin/exchangeQuery.jsp");

        } else {
            // 修改失败
            response.sendRedirect("/admin/error.jsp");
        }
    }

    public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收用户点击的上一页，下一页或者是指定的页数
        int pageIndex6 = Integer.parseInt(request.getParameter("pageIndex6"));

        integralExchangeDao = new IntegralExchangeDaoImpl();

        // 积分兑换分页查询,默认第一页
        request.getSession().setAttribute("exchanges",integralExchangeDao.queryPage(pageIndex6));
        // 积分兑换分页总页数
        request.getSession().setAttribute("exchangePage",integralExchangeDao.exchangePage());
        // 积分兑换存储是第几页
        request.getSession().setAttribute("currentPage6",pageIndex6);

        // 地址重定向
        response.sendRedirect("admin/exchangeQuery.jsp");

    }
}
